package com.viboo.concern.controller;

import com.viboo.concern.entity.Rank;
import com.viboo.concern.response.RankResult;
import com.viboo.concern.response.Result;
import com.viboo.concern.response.ResultUtil;
import com.viboo.concern.service.impl.RankServiceImpl;
import com.viboo.concern.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author avanline
 * @descriotion
 * @date 2018-03-02 9:42
 */
@RestController
@RequestMapping("rank")
public class RankController {
    @Autowired
    RankServiceImpl rankService;

    @GetMapping("select")
    Result<?> getRank(Rank rank) {
        if (TextUtils.isEmpty(rank.getUserId()))
            return ResultUtil.error("用户ID为空");
        if (TextUtils.isEmpty(rank.getOpenId()))
            return ResultUtil.error("关注人ID为空");
        try {
            RankResult mRankResult = new RankResult();
            mRankResult.setType(rank.getType());
            boolean mContainUserId = false;
            List<Rank> mRankList = rankService.selectAllRank(rank);
            if (mRankList == null || mRankList.size() == 0) {
                mRankResult.setType(-2);
                return ResultUtil.success(mRankResult);
            }
            if (mRankList.size() < 5)
                mRankResult.setType(-1);
            int ranking = 0;
            for (Rank m : mRankList) {
                ranking = ranking + 1;
                m.setRanking(ranking);
//                long time = m.getEntryTime() - rank.getShareTime();
//                m.setDate(getDifferenceTime(time));
                m.setDate(longToString(m.getEntryTime()));
                m.setUserName(URLDecoder.decode(m.getUserName(), "utf-8"));
                m.setUserId(rank.getOpenId());
                if (m.getOpenId().equals(rank.getOpenId()))
                    mContainUserId = true;
            }
            mRankResult.setRankList(mRankList);
            if (rank.getType() == 0 && !mContainUserId && !rank.getOpenId().equals(rank.getUserId())) {
                Rank mRank = rankService.selectUser(rank);
                if (mRank == null)
                    return ResultUtil.error("查询自己排名数据失败");
//                long time = mRank.getEntryTime() - rank.getShareTime();
//                mRank.setDate(getDifferenceTime(time));
                mRank.setDate(longToString(mRank.getEntryTime()));
                mRank.setRanking(mRank.getRankNo());
                mRank.setUserName(URLDecoder.decode(mRank.getUserName(), "utf-8"));
                List<Rank> meRankList = new ArrayList<>();
                meRankList.add(mRank);
                mRankResult.setMeRank(meRankList);
            } else
                mRankResult.setMeRank(new ArrayList<>());
            try {
                mRankResult.setConcernIndex(rankService.concernIndex(rank.getUserId()));//查询关心指数
            } catch (Exception e) {
                e.printStackTrace();
                mRankResult.setConcernIndex(0);
            }
            return ResultUtil.success(mRankResult);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    private String longToString(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.CHINESE);
        return simpleDateFormat.format(new Date(time));
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     */
    private String getDifferenceTime(long time) {
        long day;
        long hour;
        long min;
        long sec;
        long ms;
        day = time / (24 * 60 * 60 * 1000);
        hour = (time / (60 * 60 * 1000) - day * 24);
        min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        ms = (time - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - sec * 1000);
        StringBuilder mStringBuffer = new StringBuilder();
        if (day > 0) {
            mStringBuffer.append(day);
            mStringBuffer.append("天");
        }

        if (hour > 0) {
            mStringBuffer.append(hour);
            mStringBuffer.append("时");
        }
        if (min > 0) {
            mStringBuffer.append(min);
            mStringBuffer.append("分");
        }
        if (sec < 0)
            sec = 0;
        double mSecond = sec;
        if (ms > 0)
            mSecond = sec + ms / 1000f;
        DecimalFormat format = new DecimalFormat("0.0");//不足1位,会以0补足.
        mStringBuffer.append(format.format(mSecond));
        mStringBuffer.append("秒");
        return mStringBuffer.toString();
    }

}
